<?php

ini_set('include_path', '../');

require_once("util/configuration_parametters.php");

require_once("model/autor_ping/to/autor_ping_to.php");
require_once("model/autor_ping/dao/autor_ping_dao_factory.php");

require_once("model/autor/to/autor_to.php");
require_once("model/autor/dao/autor_dao_factory.php");


class Autor_ping_dao_test {

	private $dao;
	private $autor1;
	private $autor2;
	
	public function __construct() {}
	
	public function before_test() {			
		$this->autor1 = (object) Autor_dao_factory::singleton()->get_dao()->create(new Autor_to('xxxxxxxxxxxx','password','email'));		
		$this->autor2 = (object) Autor_dao_factory::singleton()->get_dao()->create(new Autor_to('xxxxxxxxxxxY','password','email'));		
		
		
		$this->dao = Autor_ping_dao_factory::singleton()->get_dao();
	}
	
	public function run_test() {
		try {
			$date = date("Y-m-d H:i:s");
			$autor_ping_to = new Autor_ping_to($this->autor1->get_login(), 
													$this->autor2->get_login(), 'comment', 
													'positive', $date, 'open');
			//create
			$autor_ping_to = $this->dao->create($autor_ping_to);	
			echo 'create article: ' . $autor_ping_to->to_string() . '<br/>';

			//create existent 
			try {
				$this->dao->create($autor_ping_to);
			}
			catch (Exception $e) {
				echo 'Correct error to insert autor_ping: ' . $autor_ping_to->to_string() . '<br/>';
			}
			
			//exist inexistent 
			$this->dao->exist("xxxxxxxxxxx", "yyyyyyyyyyyy");
			echo 'No exist autor_ping "xxxxxxxxxxx", "yyyyyyyyyyyy<br/>';
			
			//exist existent 
			$this->dao->exist($autor_ping_to->get_to_autor_login(), 
									$autor_ping_to->get_from_autor_login());
			echo 'exist autor_ping: ' . $autor_ping_to->to_string() . '<br/>';
			
			//find inexistent 
			try {
				$this->dao->find_by_to_autor_and_from_autor_login("xxxxxxxxxxx", "yyyyyyyyyyyy");
			}
			catch (Exception $e) {
				echo 'Correct error to find autor_ping "xxxxxxxxxxx", "yyyyyyyyyyyy"<br/>';
			}
			
			//find existent 
			$aux_autor_ping_to = $this->dao->find_by_to_autor_and_from_autor_login($autor_ping_to->get_to_autor_login(), 
																		$autor_ping_to->get_from_autor_login());	
				if ($autor_ping_to->equals($aux_autor_ping_to)) 
					echo 'found autor_ping: ' . $autor_ping_to->to_string() . '<br/>';
				else
					echo 'not equal autor_ping inserted';
													
			//update 
			$autor_ping_to->set_state("wait");
			$this->dao->update($autor_ping_to);
			$aux_autor_ping_to = $this->dao->find_by_to_autor_and_from_autor_login($autor_ping_to->get_to_autor_login(), 
																		$autor_ping_to->get_from_autor_login());	
			echo 'update autor_ping: ' . $aux_autor_ping_to->to_string() . '<br/>';

			//remove inexistent 	
			try {
				$this->dao->remove("xxxxxxxxxxx", "yyyyyyyyyyyy");
			}
			catch (Exception $e) {
				echo 'Correct error to remove autor_ping "xxxxxxxxxxx", "yyyyyyyyyyyy"<br/>';
			}
			
			//remove existent 
			$this->dao->remove($autor_ping_to->get_to_autor_login(), $autor_ping_to->get_from_autor_login());
			echo 'remove autor_ping: ' . $autor_ping_to->to_string() . '<br/>';
					
		}
		catch (Exception $e) {
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function after_test() {
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor1->get_login());
		Autor_dao_factory::singleton()->get_dao()->remove($this->autor2->get_login());
	}
}

//test
$test = new Autor_ping_dao_test();

$test->before_test();
$test->run_test();
$test->after_test();

?>